<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-09-03 21:18
 *
 * Version: 2.0
 */

namespace App\Modules\System\Repositories;


use App\Models\MyVisitHistory;
use Flower\Core\Repository\Eloquent\BaseRepository;

class MyVisitHistoryRepository extends BaseRepository
{
	public function model()
	{
		return MyVisitHistory::class;
	}

	public function add($userId, $storeId, $memberId, $source, $linkName)
	{
		$data = [
			'user_id' => $userId,
			'store_id'  => $storeId,
			'member_id' => $memberId,
			'source'    => $source,
		];

		$res =  $this->model->where($data)->whereDate('visit_time',date("Y-m-d"))->first();
		if(isset($res)){
			$res->visit_time= time();
			$res->link_name = $linkName;
			$res->count+=1;
			$res->save();
		}else{
			$data['visit_time'] = time();
			$data['link_name'] = $linkName;
			$data['count'] = 1;
			$res = $this->create($data);
		}
		return $res;
	}



	/**
	 * 写入订单访客表转化率
	 * @param $storeId
	 * @param $memberId
	 * @param $date
	 * @return mixed
	 */
	public function updateVisitor($storeId,$memberId,$userId,$date){
		$info = $this
			->model
			->whereRaw('store_id ="'.$storeId.'" and user_id="'.$userId.'" and member_id="'.$memberId.'" and DATE_FORMAT(visit_time,\'%Y-%m-%d\') ="' . $date . '"')
			->first();
		if(isset($info)){
			$info->is_order = 1;
			$info->save();
		}
		return $info;
	}
}